package com.kfm.wiki.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.kfm.wiki.entity.Ebook;
import com.kfm.wiki.service.EbookService;
import com.kfm.wiki.util.PageReq;
import com.kfm.wiki.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/ebook")
public class EBookController {

    @Autowired
    private EbookService ebookService;

    @GetMapping("/list")
    public R list(Ebook ebook, @Valid PageReq page){
        Page<Ebook> list = ebookService.page(ebook, page);
        return R.ok(list.toPageInfo());
    }

    @PostMapping("/save")
    public R save(@RequestBody @Valid Ebook ebook){
        boolean flag = ebookService.save(ebook);
        if (flag) {
            return R.ok("保存成功");
        }
        return R.fail("保存失败");
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        boolean flag = ebookService.removeById(id);
        if (flag) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
